package com.example.service.impl;

import com.example.dao.SysUserDao;
import com.example.entity.MyUserDetails;
import com.example.entity.SysUser;
import com.example.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：songxusheng
 * @description：
 * @date ：Created in 2022/5/16 9:00
 * @version:
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private SysUserDao userDao;

    @Autowired
    private SysUserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("1");
//        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
//        wrapper.eq("username",username);
//        SysUser user = userDao.selectOne(wrapper);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123456");
        SysUser user = new SysUser(1,"张三",encode,null,null,null,null,null,null,null);
        if (user == null) {
            throw new UsernameNotFoundException("用户名或密码不正确!");
        }
        //从数据库查出的用户信息id,username,password,authorityList
//        List<GrantedAuthority> authorityList = getUserAuthority(user.getId());
        return new MyUserDetails(user.getId(), user.getUsername(), user.getPassword(), null);
    }
    public List<GrantedAuthority> getUserAuthority(Integer id) {
        return AuthorityUtils.commaSeparatedStringToAuthorityList(
                userService.getUserAuthorityInfo(id)
        );
    }

}

